#include "constants.h";

XfMissionChapterExecute={
	private ["_chapter_succeeded"];
	diag_log "";
	diag_log "=========================================================================";
	diag_log "                             CHAPTER 1                                   ";
	diag_log "=========================================================================";
	diag_log "";
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterExecute","Starting Chapter","_last_random_reinforcement","_random_reinf_diff","_reinforcement_power"];
	DM_Mission_Chapter_CanRun = True;
	DM_Mission_Chapter_Runs = True;
	DM_Mission_Chapter_Success = 0;
	call XfMissionChapterSetup;
	_chapter_succeeded = false;
	if (!DM_Mission_Mission_CanRun) exitWith {diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterSetup","Mission canceled!"];};
	
	// We need the castle infos to send paras to the right place :)
	_random_town = DM_Chapter_Location;
	_random_town_pos = _random_town select XF_MAP_LOCATION_CENTER;
	_random_town_size = _random_town select XF_MAP_LOCATION_SIZE;
	if (_random_town_size < 200) then { _random_town_size = 200; };
	_building = DM_Target;
	_surrounding_buildings = DM_Target nearObjects ["House",80];
	for [{_i = 0},{_i < count _surrounding_buildings},{_i = _i + 1}] do {
		if ((count ([(_surrounding_buildings select _i),"HIDE", false] call XfBuildingPosArray) <= 0)
		     || (((_surrounding_buildings select _i) distance _building) < 20)) then {_surrounding_buildings set [_i, "XxDeLxX"]};
	};
	_surrounding_buildings = _surrounding_buildings - ["XxDeLxX"];
	
	_bomb_Defused = false;
	_bomb_Defused_updated = false;
	_bomb_detonated = false;
	_bomb_detonated_updated = false;
	_bomb_moved = false;
	_near_bomb = false;
	_near_bomb_done = false;
	_escapeTime = time;
	_escapeSlow = false;
	
	_player_detected = false;
	_player_decteted_updated = false;
	_next_mortar = 0;
	_enemy_initial_count = ([_random_town_pos,350,d_enemy_side] call XfMissionGetZoneSideCount) select 0;

	_unique_id = 0;
	_reinforcements = (call XPlayersNumber) * DM_Mission_RandomReinforcement;
	if (_reinforcements < 2) then { _reinforcements = 2; };
	diag_log Format["(%1)[%2]: %3: %4: %5, %6",diag_tickTime,"chapter1.sqf","XfMissionChapterExecute","Entering the loop",[_reinforcements, _enemy_initial_count, _surrounding_buildings], Time];
	while {DM_Mission_Chapter_CanRun AND DM_Mission_Mission_CanRun} do {
		sleep 10;
		_enemy_count = ([_random_town_pos,350,d_enemy_side] call XfMissionGetZoneSideCount) select 0;
		_players_in_town = [];
		if (X_MP) then {
			{
				if (isPlayer _x) then {
					if ([getPos _x, getPos _building, _random_town_size * 2] call XfPointInCircle) then { _players_in_town set [count _players_in_town, _x]};
					if ((_x distance (getPos _building)) < 100) then { _near_bomb = true; };
				};
			} foreach playableUnits;
		} else {
			if ([getPos player, getPos _building, _random_town_size * 3] call XfPointInCircle) then { _players_in_town set [count _players_in_town, player] };
		};
		
		_bomb_Defused = ["jip_bomb_Defused",false] call XNetGetJIP;
		_bomb_detonated = ["jip_bomb_detonated",false] call XNetGetJIP;
		_player_detected = ["jip_player_detected",false] call XNetGetJIP;

		if (_near_bomb && !_near_bomb_done) then {
			_near_bomb_done = true;
		};
		
		if (_bomb_Defused_updated && (count _players_in_town) <= 0) then {
			["ExtractGroup","", "","",[],"Succeeded"] call XfMissionTask;
			"STR_MF_GROUPISSAFE" call XfMissionMessageToAll;
			sleep 10;
		};
		
		if (_bomb_Defused_updated && ((time - _escapeTime) > 120)) then {
			if (count _players_in_town > 0) then {
				if (count _random_point > 0 && (_next_mortar < time)) then {
					_player = _players_in_town call XfRandomArrayVal;
					_random_point = [position _player, 70, 50] call XfGetRanPointCircle;
					if (!_escapeSlow) then {
						_escapeSlow = true;
						"STR_MF_MORTARINBOUND" call XfMissionMessageToAll;
					};
					_next_mortar = time + 50;
					["ARTY_Sh_82_HE", _random_point, 20, 12, 5] call R3F_FNCT_Bomber;
				};
			};
		};
		
		if (_bomb_Defused && !_bomb_Defused_updated) then {
			_bomb_Defused_updated = true;
			["DefuseBomb","", "","",[],"Succeeded"] call XfMissionTask;
			// "STR_MF_BOMBDEFUSED" call XfMissionMessageToAll;
			["ExtractGroup","STR_MF_M_GAMEOVER_TASK_EXTRACT", ["STR_MF_M_GAMEOVER_TASK_EXTRACT_DESC",_random_town_name],"",[],"Created"] call XfMissionTask;
			"STR_MF_LEAVEASAP" call XfMissionMessageToAll;
			_escapeTime = time;

			// Generate extra groups if needed
			_minimum_count = 7 * (call XPlayersNumber);
			if (_minimum_count > 30) then { _minimum_count = 30; };
			diag_log Format["(%1)[%2]: %3: %4: %5, %6",diag_tickTime,"chapter1.sqf","XfMissionChapterExecute","Generate Groups",[_minimum_count, _enemy_count, _enemy_initial_count], Time];
			if (_enemy_count < _minimum_count) then {
				_grp_cnt = (ceil ((_minimum_count - _enemy_count + (call XPlayersNumber)) / 7)) max 2;
				_positions = [];
				{
					_positions = [_positions, ([_x,"HIDE", false] call XfBuildingPosArrayASL)] call XfArrayPushStack;
				} foreach _surrounding_buildings;
				
				for [{_i = 0},{_i < _grp_cnt},{_i = _i + 1}] do {
					_i_tries = 20;
					_pos = [];
					while { count _pos <= 0 && _i_tries > 0 } do {
						_pos = (_positions call XfRandomArrayVal);
						_pos_ground = _pos set [2,0];
						{
							if (isPlayer _x) then {
								if (([_x, _pos] call XfDistTo) < 15) exitWith {_pos = [] };
							};
						} foreach playableUnits;
						_i_tries = _i_tries - 1;
					};
					if (count _pos > 0) then {
						// [false,[format["grp_reinf_%1",_i],_pos, "ICON", "ColorBlue", [0.5,0.5],"",0,"mil_dot","",0.6]] call XfMissionMarker;
						_grpSoldiers = [d_enemy_side,[(getMarkerPos "respawn_east"),15],["chapter1_random_town_ups","noveh","aware","buildings"],floor(random(5))] call XfMissionCreateFootmenGroup;
						{
							_x setVelocity [0,0,0];
							_x setPosASL _pos;
						} foreach (units _grpSoldiers);
						diag_log Format["(%1)[%2]: %3: %4: %5, %6",diag_tickTime,"chapter1.sqf","XfMissionChapterExecute","Generated Group",[_pos, _grpSoldiers], Time];
					};
				};
				sleep 5;
			};
			[getPos _building, d_enemy_side, _random_town_size, [], 20] call XfMissionConvergeUnits;
		};

		if (_bomb_Defused && (count _players_in_town) <= 0) then {
			_chapter_succeeded = true;
			DM_Mission_Chapter_CanRun = false;
		};
		
		if (_bomb_detonated) then {
			["DefuseBomb","", "","",[],"Failed"] call XfMissionTask;
			"STR_MF_WTH" call XfMissionMessageToAll;
			sleep 30;
			_chapter_succeeded = false;
			DM_Mission_Chapter_CanRun = false;
		};
		
		if (!_bomb_moved && (_bomb_Defused || _bomb_detonated)) then {
			_bomb_moved = true;
			if (!isnull DM_VIP) then {
				DM_VIP setPos [0,0,0];
			};
		};

		if (DM_Mission_Chapter_CanRun && _player_detected && !_player_decteted_updated) then {
			_player_decteted_updated = true;
			_minimum_count = (floor((call XPlayersNumber) / 3) + 1);
			[d_enemy_side,[(getPos _building),100],"VEH",_minimum_count,XF_VEHICLE_TYPES_LIGHTWHEEL] call XfMissionCallReinforcement;
		};
		
		If (DM_Mission_Chapter_CanRun AND (DM_Mission_RandomReinforcement > 1) AND (_reinforcements > 0)) then {
			// If the player is detected and no reinforcement is on the way.. Then we send new troops in :)
			if ((_enemy_initial_count - _enemy_count) > 5) then {
				if (_unique_id == 0) then {
					// If the reinforcement function returns a value greater than 0, it means a reinforcement was created!
					_unique_id = [d_enemy_side,[_random_town_pos,_random_town_size],"PARA",DM_Mission_RandomReinforcement] call XfMissionCallReinforcement;
					if ((_unique_id) > 0) then {
						diag_log Format["(%1)[%2]: %3: %4: reinf:%5, diff:%6, ini:%7, vec:%8",diag_tickTime,"chapter1.sqf","XfMissionChapterExecute","Sending reinforcement",_reinforcements,_enemy_diff,_enemy_initial_counts];
						_reinforcements = _reinforcements - 1;
						hint "Reinforcement";
					};
				};
			} else {
				_unique_id = 0;
			};
		};
	};
	
	if (DM_Mission_Mission_CanRun) then {
		if (_chapter_succeeded) then {
			call XfMissionChapterSuccess;
		} else {
			call XfMissionChapterFailure;
		};
	} else {
		diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterExecute","Mission canceled!"];
	};
	
	_players_found = true;
	_enemy_count = 100;
	While {DM_Mission_Mission_CanRun && _enemy_count > 3 && _players_found} do {
		sleep 5;
		if (X_MP) then {
			_players_found = false;
			{
				if (([(getPos _x), _random_town_pos, 600] call XfPointInCircle) && isplayer _x) exitWith { _players_found = true; };
			}foreach playableUnits;
		} else {
			_players_found = ([(getPos player), _random_town_pos, 600] call XfPointInCircle);
		};
		_enemy_count = (([_random_town_pos,400,d_enemy_side] call XfMissionGetZoneSideCount) select 0);
		if (d_mission_debug_messages) then { diag_log Format["(%1)[%2]: %3: %4: player:%5  enemies:%6",diag_tickTime,"chapter1.sqf","XfMissionChapterExecute","Chapter End Check",_players_found,_enemy_count]; };
	};
	// Just in case a reinforcement is already on the way.
	call XfMissionCancelReinforcement;
	call XfMissionChapterCleanUp;
	DM_Mission_Chapter_Runs = False;
};

XfMissionChapterSetup={
	private ["_random_town","_random_town_pos","_random_town_size","_troops_power","_troops_coeff","_grpSpecOps","_i"];
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterSetup","Starting Initialization"];
	
	_random_town = [];
	_random_town_pos = [];
	_random_town_size = 200;
	_building = objNull;
	_i_tries = 30;
	
	_building_list = [18,-1,2] call XfBuildingFilteredList;
	
	while { count _random_town <= 0 && _i_tries > 0 } do {
		// Only big towns
		_random_town = [[XF_MAP_LOCATION_TYPE_TOWN,XF_MAP_LOCATION_TYPE_VILLAGE,XF_MAP_LOCATION_TYPE_MILITARY,XF_MAP_LOCATION_TYPE_ISLAND,XF_MAP_LOCATION_TYPE_INDUSTRIAL],150] call XfMissionGetRandomSpot;
		_random_town_pos = _random_town select XF_MAP_LOCATION_CENTER;
		_random_town_size = _random_town select XF_MAP_LOCATION_SIZE;
		_building = objNull;
		_matching_buildings = [];
		
		_objects = nearestObjects [_random_town_pos, _building_list, _random_town_size];
		if (isnull _building && (count _objects > 0)) then {
			{
				_surrounding_count = 0;
				_bld = _x;
				_pos_bld = getPos _bld;
				if (damage _bld < 0.2) then {
					_surrounding_buildings = _pos_bld nearObjects ["House",80];
					{
						if (_x != _bld) then {
							if (count ([_x,"HIDE", false] call XfBuildingPosArray) > 0) then { _surrounding_count = _surrounding_count + 1; };
						};
					} foreach _surrounding_buildings;
					if (_surrounding_count > 3) then {
						_matching_buildings set [count _matching_buildings, _bld];
					};
				} else {
					diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterSetup",Format["Ignoring destroyed building in %1", (_random_town select XF_MAP_LOCATION_DISPLAYNAME)]];
				};
			} foreach _objects;
		};
		diag_log Format["(%1)[%2]: %3; %4: %5",diag_tickTime,"chapter1.sqf","XfMissionChapterSetup","Matching buildings", _matching_buildings];
		if (count _matching_buildings > 0) exitWith {
			_building = _matching_buildings call XfRandomArrayVal;
		};
		if (!isnull _building) exitWith {};
		_random_town = [];
		_i_tries = _i_tries - 1;
	};
	if ((count _random_town) <= 0) exitWith {"STR_MF_RANDOMSITEFAILED" call XfMissionMessageToAll; DM_Mission_Mission_CanRun = false; DM_Mission_Chapter_CanRun = false;};
	if (isNull _building) exitWith {"STR_MF_RANDOMBUILDINGFAILED" call XfMissionMessageToAll; DM_Mission_Mission_CanRun = false; DM_Mission_Chapter_CanRun = false;};
	_random_town_name = _random_town select XF_MAP_LOCATION_DISPLAYNAME;
	DM_Chapter_Location = _random_town;
	DM_Chapter_Position = _random_town_pos;
	DM_Target = _building;
	
	if (_random_town_size < 200) then { _random_town_size = 200; };
	// UPSMON Marker
	[true,["chapter1_random_town_ups", _random_town_pos, "ELLIPSE", "ColorRed", [_random_town_size,_random_town_size],"",0,"","BDiagonal",0.5]] call XfMissionMarker;
	[true,["chapter1_random_town_ups_building",(getPos _building), "ELLIPSE", "ColorRed", [90,90],"",0,"","FDiagonal",0.5]] call XfMissionMarker;
	[true,["chapter1_random_town_ups_building_big",(getPos _building), "ELLIPSE", "ColorRed", [200,200],"",0,"","FDiagonal",0.5]] call XfMissionMarker;

	// This indicates where the mission takes place
	[_random_town_pos,_random_town_size,_random_town_name] call XfMissionCurrentPosition;
	// We search a spot to place the teleporter which is around the town on a road (to avoid mountains and forests)
	_teleporter = [_random_town_pos] call XfMissionRandomStartTeleporter;
	// If no spot found, we get back an empty position.
	if (count (_teleporter select 0) > 0) then {
		[false,["chapter1_start_position",(_teleporter select 0), "ICON", "ColorBlue", [1,1],"",(_teleporter select 1),"hd_start","",1]] call XfMissionMarker;
	};

	["jip_bomb_Defused",false] call XNetSetJIP;
	["jip_bomb_detonated",false] call XNetSetJIP;
	_bomb_position = [];
	
	_building_pos_hiding = [_building,"HIDE", false] call XfBuildingPosArrayASL;
	_building_pos_all = [_building,"INTERN", false] call XfBuildingPosArrayASL;
	diag_log Format["(%1)[%2]: %3; %4: %5",diag_tickTime,"chapter1.sqf","XfMissionChapterSetup","Building Position",_building_pos_all];
	// We create the bomb
	_filtered_pos = +_building_pos_hiding;
	// We try to filter out the ground positions to force players to go upstairs
	for [{_i = 0},{_i < count _filtered_pos},{_i = _i + 1}] do {
		_hiding_pos = _filtered_pos select _i;
		if ((_hiding_pos select 2) < 2) then {_filtered_pos set [_i, "XxDeLxX"]};
	};
	_filtered_pos = _filtered_pos - ["XxDeLxX"];
	// It's possible we had only ground positions, in such a case we will use them.
	if (count _filtered_pos > 0) then {
		_bomb_position = _filtered_pos call XfRandomArrayVal;
	} else {
		_bomb_position = _building_pos_hiding call XfRandomArrayVal;
	};
	// We don't want to place units ON the bomb :)
	for [{_i = 0},{_i < count _building_pos_all},{_i = _i + 1}] do {
		if (((_building_pos_all select _i) distance _bomb_position) < 1) then { _building_pos_all set [_i, "XxDeLxX"] };
	};
	_building_pos_all = _building_pos_all - ["XxDeLxX"];
	
	_fnc_detonate = {
		private ["_barrel","_bomb","_pos_asl","_Defused"];
		diag_log Format["(%1)[%2]: %3; %4: %5",diag_tickTime,"chapter1.sqf","_fnc_detonate","Start",_this];
		_Defused = ["jip_bomb_Defused",false] call XNetGetJIP;
		if (!_Defused) then {
			_barrel = (_this select 0);
			_damage = (_this select 2);
			if (_damage > 0.2) then {
				_pos_asl = getPosASL _barrel;
				_barrel removeAllEventHandlers "HandleDamage";
				deleteVehicle _barrel;
				_bomb = "Bo_GBU12_LGB_F35" createVehicle [0,0,1000];
				_bomb setPosASL _pos_asl;
				_bomb setDamage 1;
				["jip_bomb_detonated",true] call XNetSetJIP;
			};
		};
	};
	_bomb_position set [2, (_bomb_position select 2) + 0.1];
	_barrel = ["Barrel4",_bomb_position,"REGISTER",[]] call XfMissionCreateMissionObject;
	_barrel setPosASL _bomb_position;
	_barrel addEventHandler ["HandleDamage", _fnc_detonate];
	diag_log Format["(%1)[%2]: %3; %4: %5",diag_tickTime,"chapter1.sqf","XfMissionChapterSetup","Barrel Position",[_bomb_position, typeof(_barrel)]];
	_bomb_position = getPosASL _barrel;
	_bomb_position set [2, (_bomb_position select 2) + 0.8];
	DM_VIP = ["Explosive",[0,0,0],"ACTIVATE",["STR_MF_M_GAMEOVER_BOMB_DEACT","STR_MF_M_GAMEOVER_BOMB_ACT","jip_bomb_Defused","","STR_MF_M_GAMEOVER_BOMB_ACTION"]] call XfMissionCreateMissionObject;
	DM_VIP setPosASL _bomb_position;
	diag_log Format["(%1)[%2]: %3; %4: %5",diag_tickTime,"chapter1.sqf","XfMissionChapterSetup","Bomb Position",[_bomb_position, typeof(DM_VIP)]];
	DM_VIP setPosASL _bomb_position;

	_statics = [5, d_enemy_side, 20] call XfMissionGetEnemyStaticsArray;
	_grp_statics = [d_enemy_side] call x_creategroup;
	for [{_i = 0},{_i < 4},{_i = _i + 1}] do {
		_pos_static = position _building;
		_dir_static = random(360);
		switch ( _i ) do {
			case 3: {
					_pos_static = _building modelToWorld [20,0,0];
					_dir_static = 90;
				};
			case 2: {
					_pos_static = _building modelToWorld [-20,0,0];
					_dir_static = 270;
				};
			case 1: {
					_pos_static = _building modelToWorld [0,20,0];
					_dir_static = 0;
				};
			default {
					_pos_static = _building modelToWorld [0,-20,0];
					_dir_static = 180;
				};
		};
		_pos_static = _pos_static findEmptyPosition [0, 10];
		if ((count _pos_static) > 0) then {
			diag_log Format["(%1)[%2]: %3; %4: %5",diag_tickTime,"chapter1.sqf","XfMissionChapterSetup","Create Static",_pos_static];
			[(_statics call XfRandomArrayVal),false,_grp_statics,[_pos_static,_dir_static],["chapter1_random_town_ups","combat","RED"]] call XfMissionCreateUnitGroup;
		};
	};
	DM_VIP setPosASL _bomb_position;
	
	_fnc_KeepInPlace = {
		_unit = _this select 0;
		_unit setUnitPos "Middle";
		_initial_pos = getPos _unit;
		doStop _unit;
		while { alive _unit } do {
			if ((_unit distance _initial_pos) > 2) then { _unit doMove _initial_pos; };
			sleep 1.23456;
		};
	};
	
	_grp_cnt = floor(2 + DM_Mission_EnemyPower);
	for [{_i = 0},{_i < _grp_cnt},{_i = _i + 1}] do {
		_grpSoldiers = [d_enemy_side,[(getPos _building),20],["chapter1_random_town_ups","noveh","aware","buildings","fortify"],floor(random(5))] call XfMissionCreateFootmenGroup;
		_wp = _grpSoldiers addWaypoint [(getPos _building),20];
		{
			if (count _building_pos_all > 0) then {
				_idx = (_building_pos_all call XfRandomFloorArray);
				_pos = _building_pos_all select _idx;
				_building_pos_all set [_idx, "XxDeLxX"];
				_building_pos_all = _building_pos_all - ["XxDeLxX"];
				_x setPosASL _pos;
				_x addEventHandler ["HandleDamage",{_dmg = _this select 2; { _x enableAI "MOVE"; } foreach (units (group (_this select 0)));  _dmg;}];
				[_x] spawn _fnc_KeepInPlace;
			} else {
				_pos = (getPos _building) findEmptyPosition [20, 80];
				if (count _pos > 0) then { _x setPos _pos; };
			};
		} foreach units _grpSoldiers;
	};
	_grpSoldiers = [d_enemy_side,[(getPos _building),80],["chapter1_random_town_ups_building","noveh","aware","buildings"],XF_UNIT_TYPES_SPECIAL_FORCE] call XfMissionCreateFootmenGroup;
	

	_grp_cnt = 1 + round((call XPlayersNumber) / 5) + floor(DM_Mission_EnemyPower + DM_Mission_EnemyCoeff);
	for [{_i = 0},{_i < _grp_cnt},{_i = _i + 1}] do {
		_ups_zone = "chapter1_random_town_ups";
		if (random(1) >= 0.5) then { _ups_zone = "chapter1_random_town_ups_building_big"; };
		_grpSoldiers = [d_enemy_side,[getMarkerPos _ups_zone,(getMarkerSize _ups_zone) select 0],[_ups_zone,"noveh","safe","buildings"],floor(random(5))] call XfMissionCreateFootmenGroup;
	};
	
	// The marker which indicates the buildings zone
	[false,["chapter1_random_town_zone", _random_town_pos, "ELLIPSE", "ColorRed", [_random_town_size,_random_town_size],"",0,"","BDiagonal",0.5]] call XfMissionMarker;
	[false,["chapter1_building_position",getPos _building, "ICON", "ColorRed", [1,1],"Building",0,"hd_objective","",0.6]] call XfMissionMarker;
	
	["DefuseBomb","STR_MF_M_GAMEOVER_TASK_BOMB", ["STR_MF_M_GAMEOVER_TASK_BOMB_DESC",_random_town_name],"",[],"Created"] call XfMissionTask;
	
	["jip_player_detected",false] call XNetSetJIP;
	[(getPos _building),100,d_own_side,d_enemy_side,"jip_player_detected"] call XfMissionSideDetected;

	// This will create air support automatically if selected in the mission setup
	[d_enemy_side,[_random_town_pos,750],DM_Mission_AirSupport] call XfMissionCallAirSupport;
	
	if (typename DM_Mission_Snipers == typename []) then {
		DM_Mission_Snipers set [0, (DM_Mission_Snipers select 0) + 3];
		DM_Mission_Snipers set [1, (DM_Mission_Snipers select 1) + 3];
	} else {
		DM_Mission_Snipers = DM_Mission_Snipers + 3;
	};
	// This will create snipers automatically if selected in the mission setup
	[d_enemy_side,[_random_town_pos,_random_town_size],DM_Mission_Snipers] call XfMissionCreateSnipersArea;
	
	_amount = round(random(_random_town_size / 14)) + round(_random_town_size / 17);
	[[_random_town_pos,_random_town_size],"CIV",_amount] call XfMissionCivilianSpawn;
};

XfMissionChapterSuccess={
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterSuccess","Starting"];
	"STR_MF_MISSIONSUCCES3" call XfMissionMessageToAll;
	DM_Mission_Chapter_Success = 1;
};

XfMissionChapterFailure={
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterFailure","Starting"];
	"STR_MF_MISSIONFAILURE1" call XfMissionMessageToAll;
	DM_Mission_Chapter_Success = -1;
};

// Self-explanatory
XfMissionChapterCleanUp={
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterCleanUp","Starting"];
	// Giving an empty position places the teleporter back to the hangar, closing the hangar.
	[[],0] call XfMissionPlaceStartTeleporter;
	[] call XfMissionCleanMarkers;
	[] call XfMissionCleanTriggers;
	[] call XfMissionCleanTasks;
};

DM_Mission_Chapter_Compiled = true;